package com.clkj.sunhelp.mvp.areachoose;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.adapter.list.ListAdapterForAreaChoose;
import com.clkj.sunhelp.app.CustomApplication;
import com.clkj.sunhelp.bean.AreaItem;
import com.clkj.sunhelp.common.Config;
import com.clkj.sunhelp.mvp.base.BaseDialogFragment;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.schedulers.SchedulerProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * 收获地址省市区县选择
 * Created by Administrator on 2016/4/29.
 */
public class DialogFragmentAreaChoose extends BaseDialogFragment implements AreaChooseContract.View{

    public static final String PARENTAREAID = "parentareaid";
    public static final String TITLE = "title";
    public static final String TYPE = "type";
    private String mParentAreaId;
    private String mTitle;
    private int mType;

    public static DialogFragmentAreaChoose getInstance(String parentAreaId, String title, int type)
    {
        DialogFragmentAreaChoose dialogFragmentAreaChoose = new DialogFragmentAreaChoose();
        Bundle bundle = new Bundle();
        bundle.putString(PARENTAREAID,parentAreaId);
        bundle.putString(TITLE,title);
        bundle.putInt(TYPE,type);

        dialogFragmentAreaChoose.setArguments(bundle);
        return dialogFragmentAreaChoose;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null!=bundle)
        {
            mParentAreaId = bundle.getString(PARENTAREAID);
            mTitle = bundle.getString(TITLE);
            mType = bundle.getInt(TYPE);
        }
        mPresenter = new AreaChoosePresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }

    private TextView tvTip;
    private ListView lvArea;

    AreaChooseContract.Presenter mPresenter;


    List<AreaItem> mAreaList = new ArrayList<>();
    ListAdapterForAreaChoose mAdapter;

    AreaChooseListener mAreaChooseListener;

    public AreaChooseListener getAreaChooseListener() {
        return mAreaChooseListener;
    }

    public void setAreaChooseListener(AreaChooseListener areaChooseListener) {
        mAreaChooseListener = areaChooseListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.dialog_frag_area_choose,container,false);
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        assignView(view);
        initView();
        getData();

    }

    @Override
    public void initData() {

    }


    @Override
    public void assignView(View view) {


        tvTip = (TextView) view.findViewById(R.id.tv_tip);
        lvArea = (ListView) view.findViewById(R.id.lv_area);

    }

    @Override
    public void initView() {
        tvTip.setText(mTitle);
        mAdapter = new ListAdapterForAreaChoose(mAreaList,getActivity());
        lvArea.setAdapter(mAdapter);
        lvArea.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null!=mAreaChooseListener)
                {
                    mAreaChooseListener.onAreaChoose(mAreaList.get(position),mType);
                }

                dismiss();
            }
        });
    }


    private void getData()
    {
        switch (mType)
        {
            case Config.TYPE_AREA_QUXIAN:

                getAllQuXian();

                break;
            case Config.TYPE_AREA_XIANGZHEN:

                getXiangZhenByQuXianId();

                break;
            case Config.TYPE_AREA_CUNLUO:

                getCunLuoByXiangZhenId();

                break;
        }
    }

    @Override
    public void getAllQuXian() {
        mPresenter.getAllQuXian();
    }

    @Override
    public void onQetAllQuXianSuccess(List<AreaItem> areaList) {
        mAreaList.clear();
        mAreaList.addAll(areaList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onQetAllQuXianError(String tip) {
        ToastUtil.showShort(getActivity(),tip);
    }

    @Override
    public void getXiangZhenByQuXianId() {
       mPresenter.getXiangZhenByQuXianId(mParentAreaId);
    }

    @Override
    public void onGetXiangZhenByQuXianIdSuccess(List<AreaItem> areaList) {
        mAreaList.clear();
        mAreaList.addAll(areaList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetXiangZhenByQuXianIdError(String tip) {
        ToastUtil.showShort(getActivity(),tip);
    }

    @Override
    public void getCunLuoByXiangZhenId() {
        mPresenter.getCunLuoByXiangZhenId(mParentAreaId);
    }

    @Override
    public void onGetCunLuoByXiangZhenIdSuccess(List<AreaItem> areaList) {
        mAreaList.clear();
        mAreaList.addAll(areaList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetCunLuoByXiangZhenIdError(String tip) {
        ToastUtil.showShort(getActivity(),tip);
    }

    @Override
    public void setPresenter(AreaChooseContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false,null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }

   /* @Override
    public void getData() {

    }

    @Override
    public void getArea() {
        presenter.getArea(mParentId);
    }

    @Override
    public void onGetAreaSuccess(List<ReceiveAddressAreaItem> receiveAddressAreaItemList) {
        mAreaList.clear();
        mAreaList.addAll(receiveAddressAreaItemList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetAreaError(String tip) {
        ToastUtil.showShort(getActivity(),tip);
    }*/

    /*@Override
    public void showLoading() {
        showProgressDialog(false,null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }*/




    /**
     * 省市区选择监听
     */
    public interface AreaChooseListener{
        /**
         * 选择的地址
         * @param areaItem
         * @param type 选择的地址的类型
         */
        public void onAreaChoose(AreaItem areaItem, int type);
    }
}
